<?php

class HTech_Image_Thumbnail Extends HTech_Image
{    
    protected $_thumb;
    protected $_thumbMaxWidth;
    protected $_thumbMaxHeight;
    protected $_thumbQuality;
    
    public function setWidth($width)
    {
        $this->_thumbMaxWidth = $width;
        return $this;
    }
    
    public function setHeight($height)
    {
        $this->_thumbMaxHeight = $height;
        return $this;
    }
    
    public function setQuality($quality)
    {
        $this->_thumbQuality = $quality;
        return $this;
    }
       
    public function setPath($path)
    {
        $this->_path = $path;
        return $this;
    }
    
    public function create()
    {        
        $thumbAspect = $this->_thumbMaxWidth / $this->_thumbMaxHeight;
        if ($this->_width <= $this->_thumbMaxWidth && $this->_height <= $this->_thumbMaxHeight) {
            $thumbWidth = $this->_width;
            $thumbHeight = $this->_height;
        } elseif ($thumbAspect > $this->_aspect) {
            $thumbWidth = (int) ($this->_thumbMaxHeight * $this->_aspect);
            $thumbHeight = $this->_thumbMaxHeight;
        } else {
            $thumbWidth = $this->_thumbMaxWidth;
            $thumbHeight = (int) ($this->_thumbMaxWidth / $this->_aspect);
        }
        $this->_thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
        imagecopyresampled($this->_thumb, $this->_image, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $this->_width, $this->_height);
        imagejpeg($this->_thumb, $this->_path, $this->_thumbQuality);
        imagedestroy($this->_image);
        imagedestroy($this->_thumb);
    }
}